/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2016-3-22         yellowlgx
 *
 */

package com.skyworth.ui.customview.app;

import java.util.List;

import com.skyworth.commen.ui.R;
import com.skyworth.theme.widget.IThemeable;
import com.skyworth.ui.customview.SlideFocusView;
import com.skyworth.ui.listview.AdapterItem;
import com.skyworth.ui.listview.MetroAdapter;
import com.skyworth.ui.listview.MetroListView;
import com.skyworth.ui.listview.MetroListView.OnItemOnKeyListener;
import com.skyworth.ui.listview.MetroListView.OnItemSelectedListener;
import com.skyworth.ui.listview.MetroListView.OnScrollBarOnKeyListener;
import com.skyworth.ui.listview.MetroListView.OnScrollStateListener;
import com.skyworth.ui.listview.MetroListViewScrollBar;
import com.skyworth.util.SkyScreenParams;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;

/**
 * @ClassName LocalAppGridView
 * @Description TODO (write something)
 * @author yellowlgx
 * @date 2016-3-22
 * @version TODO (write something)
 */
public class CustomAppGridView extends SlideFocusView
        implements OnItemSelectedListener, OnScrollStateListener, OnItemOnKeyListener,
        OnItemClickListener, OnScrollBarOnKeyListener, IThemeable
{
    private Context mContext;
    private MetroAdapter<CustomAppInfo> mAdapter;
    private MetroListView mGridView;
    private MetroListViewScrollBar mScrollBar;

    private List<CustomAppInfo> curInfos;

    private int focusMargin = 0;

    private OnAppGridItemOnClikListener mAppGridItemOnClikListener;

    public interface OnAppGridItemOnClikListener
    {
        public void onGridItemClickEvent(View view, CustomAppInfo curInfo, int position);
    }

    public CustomAppGridView(Context context)
    {
        super(context, R.drawable.ui_sdk_btn_focus_shadow_no_bg);
        mContext = context;

        initScrollBar();

        initGridView();
    }

    public void setOnAppGridItemOnClikListener(OnAppGridItemOnClikListener listener)
    {
        mAppGridItemOnClikListener = listener;
    }

    @SuppressLint("RtlHardcoded")
    private void initScrollBar()
    {
        mScrollBar = new MetroListViewScrollBar(mContext);
        mScrollBar.setScrollBarBg(R.drawable.ic_custom_scrollbar_bg);
        mScrollBar.setScrollBarIcon(R.drawable.ic_custom_scrollbar_focus_icon);
        mScrollBar.setSlidFocusView(this);
        focusMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(75);
        int left = focusMargin;
        int top = focusMargin;
        int right = focusMargin;
        int bottom = focusMargin;
        mScrollBar.setFocusChangedEvent(focusChangedEvent,
                new FocusViewRevision(left, top, right, bottom), null);
        mScrollBar.setScrollBarFocusble(true);
        FrameLayout.LayoutParams scrollBar_p = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                SkyScreenParams.getInstence(mContext).getResolutionValue(787));
        scrollBar_p.gravity = Gravity.RIGHT;
        scrollBar_p.rightMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(72);
        scrollBar_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(189);

        this.addView(mScrollBar, scrollBar_p);
    }

    private void initGridView()
    {
        mGridView = new MetroListView(mContext);

        mGridView.setSlidFocusView(this);
        mGridView.setColmusNum(6);
        mGridView.setScrollDuration(200);
        mGridView.setScrollBarView(mScrollBar);
        FrameLayout.LayoutParams list_p = new FrameLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(1645),
                SkyScreenParams.getInstence(mContext).getResolutionValue(824));
        list_p.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(135);
        list_p.topMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(165);
        this.addView(mGridView, list_p);

        mGridView.setOnItemSelectedListener(this);
        mGridView.setOnScrollStateListener(this);
        mGridView.setOnItemOnKeyListener(this);
        mGridView.setOnItemClickListener(this);
        mGridView.setOnScrollBarOnKeyListener(this);
    }

    public void refreshValue(List<CustomAppInfo> infos)
    {
        curInfos = infos;
        mAdapter = new MetroAdapter<CustomAppInfo>(curInfos)
        {
            @Override
            public AdapterItem<CustomAppInfo> onCreateItem(Object type)
            {
                CustomAppItemView item = new CustomAppItemView(mContext);
                return item;
            }
        };

        focusMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(66);
        mAdapter.setFocusChangedEvent(focusChangedEvent,
                new FocusViewRevision(focusMargin, focusMargin, focusMargin, focusMargin), null);
        mGridView.setAdapter(mAdapter);

        stopAnimationOnce();
        mGridView.post(new Runnable()
        {
            @Override
            public void run()
            {
                mGridView.setSelection(0);
            }
        });
    }

    @Override
    public boolean onScrollBarOnKey(View view, int keyCode)
    {
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
        {
            getFocusView().setBackgroundResource(R.drawable.ui_sdk_btn_focus_shadow_no_bg);
            mScrollBar.setScrollBarIcon(R.drawable.ic_custom_scrollbar_unfocus_icon);
        }
        return false;
    }

    @Override
    public boolean onBorderItemOnKeyEvent(View view, int keyCode, int position)
    {
        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
        {
            if (mScrollBar.getVisibility() == View.VISIBLE)
            {
                getFocusView().setBackgroundResource(R.drawable.ic_custom_scrollbar_focus_bg);
                mScrollBar.setScrollBarIcon(R.drawable.ic_custom_scrollbar_focus_icon);
                CustomAppItemView appItemView = (CustomAppItemView) view;
                appItemView.unfocus();
            }
        }
        return false;
    }

    @Override
    public boolean onItemOnKeyEvent(View view, int keyCode, int position)
    {
        return false;
    }

    @Override
    public void onScrollEnd(MetroListView parent, int firstPostion, int endPosition)
    {
    }

    @Override
    public void onItemSelected(MetroListView parent, View selectView, int selectPosition)
    {
        CustomAppItemView view = (CustomAppItemView) selectView;
        view.focus();
    }

    @Override
    public void onItemUnSelected(MetroListView parent, View unSelectView)
    {
        CustomAppItemView view = (CustomAppItemView) unSelectView;
        view.unfocus();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        CustomAppItemView appItemView = (CustomAppItemView) view;
        if (mAppGridItemOnClikListener != null)
            mAppGridItemOnClikListener.onGridItemClickEvent(view, appItemView.getCurInfo(),
                    position);
    }

    @Override
    public void refreshOnThemeChanged()
    {
        if (mGridView != null)
            for (int i = 0; i < mGridView.getChildCount(); i++)
            {
                CustomAppItemView item = (CustomAppItemView) mGridView.getChildAt(i);
                item.refreshOnThemeChanged();
            }
        if (mScrollBar != null)
            mScrollBar.setScrollBarBg(R.drawable.ic_custom_scrollbar_bg);
    }

}
